package com.dexilaonian.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.bean.AgingBean;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2018/4/9.
 */

public class HomeAgingAdapter extends RecyclerView.Adapter<HomeAgingAdapter.ViewHolder> {
    private Context context;
    private  List<AgingBean> mList_aging;
    private MyItemClickListener mItemClickListener;

    public HomeAgingAdapter(Context context, List<AgingBean> list_project) {
        this.context = context;
        mList_aging = list_project;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.aging_recyclerview_item, null);
        //将全局的监听传递给holder
        ViewHolder holder = new ViewHolder(view, mItemClickListener);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String name = mList_aging.get(position).getName();
        String pic_url = mList_aging.get(position).getPic_url();
        String introduce = mList_aging.get(position).getIntroduce();
        String price = mList_aging.get(position).getOriginal_price();
        if (!TextUtils.isEmpty(name)) {
            holder.tvItemAging.setText(name);
        }
        if (!TextUtils.isEmpty(introduce)) {
            holder.minAging.setText(introduce);
        }
        if (!TextUtils.isEmpty(pic_url)) {
            String pro_url = Constant.base_pic_url + pic_url;
            ImageLoader.display2(context, pro_url, holder.recycler_aging);
        }
        if (!TextUtils.isEmpty(price)) {
            holder.agingMoney.setText("￥" + price);
        }
    }
    @Override
    public int getItemCount() {
        if (mList_aging.size() == 0) {
            return 0;
        } else {
            return mList_aging.size();
        }
    }
    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        MyItemClickListener mListener;
        TextView tvItemAging;
        TextView minAging;
        TextView agingMoney;
        ImageView recycler_aging;

        public ViewHolder(View itemView, MyItemClickListener myItemClickListener) {
            super(itemView);
            this.mListener = myItemClickListener;
            itemView.setOnClickListener(this);
            tvItemAging = (TextView) itemView.findViewById(R.id.tv_title_aging);
            minAging = (TextView) itemView.findViewById(R.id.tv_min_aging);
            agingMoney = (TextView) itemView.findViewById(R.id.aging_money);
            recycler_aging = (ImageView) itemView.findViewById(R.id.iv_recycler_aging);
        }
        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClick(v, getPosition());
            }

        }
    }
    /**
     * 创建一个回调接口
     */
    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
     *
     * @param myItemClickListener
     */
    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }
}
